import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EmployeeIDScreen.java
 *
 * Created on Dec 13, 2011, 9:05:46 PM
 */

/**
 *
 * @author dabeast23
 */
public class EmployeeIDScreen extends javax.swing.JFrame {

    /** Creates new form EmployeeIDScreen */
    public EmployeeIDScreen() {
        setResizable(false);
	setTitle("Hotel Kiosk: Enter Employee ID");
	setDefaultCloseOperation(this.EXIT_ON_CLOSE);
        initComponents();
        required.setVisible(false);
        passStar.setVisible(false);
        reenter.setVisible(false);
        this.setLocationRelativeTo(null);
        passwordField.addKeyListener(new KeyAdapter() {
	public void keyPressed(KeyEvent arg0)
	{
            //If the user presses enter, attempt to login
            if(arg0.getKeyCode()==KeyEvent.VK_ENTER)
                attemptLogin();
	}
    });
    }
    
    public EmployeeIDScreen(ArrayList reservationList) {
        setResizable(false);
	setTitle("Hotel Kiosk: Enter Employee ID");
	setDefaultCloseOperation(this.EXIT_ON_CLOSE);
        initComponents();
        this.reservationList = reservationList;
        required.setVisible(false);
        passStar.setVisible(false);
        reenter.setVisible(false);
        this.setLocationRelativeTo(null);
        passwordField.addKeyListener(new KeyAdapter() {
	public void keyPressed(KeyEvent arg0)
	{
            //If the user presses enter, attempt to login
            if(arg0.getKeyCode()==KeyEvent.VK_ENTER)
                attemptLogin();
	}
    });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        backgroundPanel = new javax.swing.JPanel();
        enterButton = new javax.swing.JButton();
        employeeIDPromptLabel = new javax.swing.JLabel();
        backButton = new javax.swing.JButton();
        passwordField = new javax.swing.JPasswordField();
        required = new javax.swing.JLabel();
        passStar = new javax.swing.JLabel();
        reenter = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        backgroundPanel.setBackground(new java.awt.Color(102, 0, 0));

        enterButton.setBackground(new java.awt.Color(255, 255, 204));
        enterButton.setFont(new java.awt.Font("Comic Sans MS", 0, 18));
        enterButton.setForeground(new java.awt.Color(102, 0, 0));
        enterButton.setText("Enter");
        enterButton.setBorder(null);
        enterButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                enterButtonActionPerformed(evt);
            }
        });

        employeeIDPromptLabel.setFont(new java.awt.Font("Tahoma", 1, 24));
        employeeIDPromptLabel.setForeground(new java.awt.Color(255, 255, 204));
        employeeIDPromptLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        employeeIDPromptLabel.setText("Enter Employee ID");

        backButton.setBackground(new java.awt.Color(255, 255, 204));
        backButton.setFont(new java.awt.Font("Comic Sans MS", 0, 11));
        backButton.setForeground(new java.awt.Color(255, 255, 255));
        backButton.setText("Back");
        backButton.setContentAreaFilled(false);
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });

        passwordField.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        passwordField.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        required.setFont(new java.awt.Font("Tahoma", 1, 14));
        required.setForeground(new java.awt.Color(255, 255, 255));
        required.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        required.setText("*Required Field Is Empty*");
        required.setEnabled(false);

        passStar.setFont(new java.awt.Font("Tahoma", 1, 14));
        passStar.setForeground(new java.awt.Color(255, 255, 255));
        passStar.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        passStar.setText("*");
        passStar.setEnabled(false);

        reenter.setFont(new java.awt.Font("Tahoma", 1, 14));
        reenter.setForeground(new java.awt.Color(255, 255, 255));
        reenter.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        reenter.setText("*Incorrect ID*");
        reenter.setEnabled(false);

        javax.swing.GroupLayout backgroundPanelLayout = new javax.swing.GroupLayout(backgroundPanel);
        backgroundPanel.setLayout(backgroundPanelLayout);
        backgroundPanelLayout.setHorizontalGroup(
            backgroundPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, backgroundPanelLayout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(passStar, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(backgroundPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(employeeIDPromptLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE)
                    .addComponent(passwordField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE))
                .addGap(65, 65, 65))
            .addGroup(backgroundPanelLayout.createSequentialGroup()
                .addGap(99, 99, 99)
                .addComponent(required)
                .addContainerGap(121, Short.MAX_VALUE))
            .addGroup(backgroundPanelLayout.createSequentialGroup()
                .addGap(111, 111, 111)
                .addGroup(backgroundPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(reenter, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(enterButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 53, Short.MAX_VALUE)
                .addComponent(backButton)
                .addContainerGap())
        );
        backgroundPanelLayout.setVerticalGroup(
            backgroundPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, backgroundPanelLayout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(employeeIDPromptLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(14, 14, 14)
                .addComponent(required)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(backgroundPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(passStar, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(reenter)
                .addGap(14, 14, 14)
                .addComponent(enterButton, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(41, 41, 41)
                .addComponent(backButton)
                .addGap(11, 11, 11))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(backgroundPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(backgroundPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_enterButtonActionPerformed
        attemptLogin();
}//GEN-LAST:event_enterButtonActionPerformed

    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backButtonActionPerformed
        ChooseUserScreen CUS = new ChooseUserScreen();
        CUS.setVisible(true);
        this.setVisible(false);
}//GEN-LAST:event_backButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new EmployeeIDScreen().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton backButton;
    private javax.swing.JPanel backgroundPanel;
    private javax.swing.JLabel employeeIDPromptLabel;
    private javax.swing.JButton enterButton;
    private javax.swing.JLabel passStar;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.JLabel reenter;
    private javax.swing.JLabel required;
    // End of variables declaration//GEN-END:variables
    public ArrayList<String> employeeIDs = new ArrayList();
    private ArrayList <Reservation> reservationList;


    private void attemptLogin()
    {
        //Hide the error labels if they are showing
		required.setVisible(false);
                passStar.setVisible(false);
		reenter.setVisible(false);

		//Load the input into temporary variables
		char[] pass = passwordField.getPassword();
                String storedPass="";

                boolean loginSuccessful=false;

		//Make sure field isn't empty
		if((pass.length>0))
		{
                    employeeIDs = readUserIDs("src/EmployeeID.txt");
                    String passString = new String(pass);
                    for (int i = 0; i < employeeIDs.size(); i++)
                    {
                        storedPass = employeeIDs.get(i);
                        if(storedPass.equals(passString))
                        {
                            loginSuccessful=true;
                        }
                    }

                    if(loginSuccessful==true)
                    {
                        System.out.println("success");
                        EmpFacingView Start = EmpFacingView.getView();
                        //Start.setVisible(true);
                        
                        Start.setVisible(true);
                        this.setVisible(false);
                    }
                    else
                    {
                        System.out.println("failure");
			passwordField.setText("");
			reenter.setVisible(true);
                    }
		}
		else
		{
			required.setVisible(true);
			if(pass.length==0)
                        {
                            passStar.setVisible(true);
                        }
		}
    }

    public ArrayList readUserIDs(String fileName)
    {
        ArrayList<String> list = new ArrayList();
        try {
            FileInputStream fis = new FileInputStream(fileName);
            DataInputStream in = new DataInputStream(fis);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strline;

            while ((strline = br.readLine()) != null) {
                list.add(strline);
                //strline = br.readLine();
            }
            in.close();
        }
        catch (IOException ex)
        {
            System.out.println("Could not read from file");
        }
        return list;
    }
}
